package com.yyk.yykywd.network

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.yyk.yykywd.bean.AlidayuSendsmsInfo
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.bean.CenterCarInfo
import com.yyk.yykywd.bean.LoginLoginInfo
import com.yyk.yykywd.config.HttpUrl
import com.yyk.yykywd.mvvm.entity.MeetingBean
import com.yyk.yykywd.mvvm.entity.SearchCustomerBean
import io.reactivex.Observable
import kotlinx.coroutines.Deferred
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.FieldMap
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST

private val okHttpClient: OkHttpClient = OkHttpClient.Builder()
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .build()

private val retrofit = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(CoroutineCallAdapterFactory())
        .baseUrl(HttpUrl.BASE_URL)
        .client(okHttpClient)
        .build()

interface BusinessApiService {
    @FormUrlEncoded
    @POST("inside/H5/card")
    fun getUserInfo(@FieldMap stringMap: Map<String, String>): Deferred<CenterCarInfo>

    @FormUrlEncoded
    @POST("inside/WorkCount/meeting_list")
    fun getMeetingsAsync(@FieldMap stringMap: Map<String, String>): Deferred<MeetingBean>

    //接口8：登录（ggk）
    @FormUrlEncoded
    @POST("inside/Login/login")
    fun loginAsync(@FieldMap stringMap: Map<String, String>): Deferred<LoginLoginInfo>

    //2：发送验证码（whm）
    @FormUrlEncoded
    @POST("inside/alidayu/sendSMS")
    fun sendSMSAsync(@FieldMap stringMap: Map<String, String>): Deferred<AlidayuSendsmsInfo>

    @FormUrlEncoded
    @POST("inside/v2/Login/update_pwd")
    fun modifyPwdAsync(@FieldMap stringMap: Map<String, String>): Deferred<BaseBean>

    @FormUrlEncoded
    @POST("inside/v2/Client/search_client")
    fun searchAsync(@FieldMap stringMap: Map<String, String>): Deferred<SearchCustomerBean>

    //接口 1:手机号密码登录(cwb)
    @FormUrlEncoded
    @POST("inside/v2/Login/login")
    fun pwdLoginAsync(@FieldMap stringMap: Map<String, String>): Deferred<LoginLoginInfo>
}

object BusinessApi {
    val retrofitService: BusinessApiService by lazy {
        retrofit.create(BusinessApiService::class.java)
    }
}